package com.mp3.player.views
{
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import flashx.textLayout.conversion.TextConverter;
	import flashx.textLayout.elements.TextFlow;
	
	import mx.controls.Alert;
	
	import spark.components.RichEditableText;
	import spark.components.supportClasses.SkinnableComponent;
	import spark.utils.TextFlowUtil;

	/**
	 * Component to show advertasing text (TLF format)
	 */	
	public class AdvertPanel extends SkinnableComponent
	{
		/**
		 * Constructor
		 */		
		public function AdvertPanel()
		{
		}
		
		[SkinPart(required="true")]
		/**
		 *  A skin part that defines the text display element for advertising conent. 
		 */
		public var textDisplay:RichEditableText;
		

		private var _source: String;
		
		[Bindable]
		/**
		 * Source to TLF xml file to show 
		 */
		public function get source(): String
		{
			return _source;
		}

		/**
		 * @private
		 */
		public function set source(value: String): void
		{
			if(_source != value)
			{
				_source = value;
				
				loadTLF();
			}
		}
		
		/**
		 * Text flow.
		 */		
		private var textFlow:TextFlow;

		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		/**
		 * Load tlf file from server 
		 */		
		private function loadTLF():void
		{
			var loader:URLLoader = new URLLoader()
			var request:URLRequest = new URLRequest(_source);
			loader.addEventListener(Event.COMPLETE, loadTLFComplete);
			loader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
			loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler);
			loader.load(request);
		}


		//--------------------------------------------------------------------------
		//
		//  Lyfe Cycle
		//
		//--------------------------------------------------------------------------
		/**
		 * @inheritDoc 
		 */		
		override protected function partAdded(partName:String, instance:Object):void
		{
			super.partAdded(partName, instance);
			
			if(instance == textDisplay && textFlow)
				textDisplay.content = textFlow;
				
		}
		//--------------------------------------------------------------------------
		//
		//  Event Handlers
		//
		//--------------------------------------------------------------------------
		/**
		 * Load complete handler.
		 *  
		 * @param event Event
		 */		
		private function loadTLFComplete(event:Event):void
		{
			textFlow = TextConverter.importToFlow(event.target.data, TextConverter.TEXT_LAYOUT_FORMAT); 
				
			if(textDisplay)
				textDisplay.content = textFlow;
		}
		
		/**
		 * Error handler.
		 *  
		 * @param event Event
		 */		
		private function errorHandler(event:Event):void
		{
			trace(event.toString());
		}
	}
}